//
//  PubulicTextView.m
//  OnionHome
//
//  Created by fengjie on 2019/9/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PubulicTextView.h"


@interface PubulicTextView ()<UITextViewDelegate>

@end

@implementation PubulicTextView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTexView) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}
-(void)changeTexView{
    DLog(@"%@",self.textView.text);
    if ([self.textView.text isEqualToString:@""]) {
        self.placeholderText.hidden = NO;
    }else{
        self.placeholderText.hidden = YES;
    }
    BlockSafeRun(self.backIntegerBlock,(NSInteger)self.textView.text.length);
}
-(void)createUI{
    [self addSubview:self.textView];
    [self.textView addSubview:self.placeholderText];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    [self.placeholderText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView.mas_top).offset(10);
        make.left.mas_equalTo(7);
        make.width.mas_equalTo(KScreenWidth-80);

    }];
}
-(UITextView*)textView{
    if(!_textView){
        _textView=[UITextView new];
        _textView.delegate = self;
        
    }
    return _textView;
}
-(UILabel*)placeholderText{
    if(!_placeholderText){
        _placeholderText=[UILabel new];
        _placeholderText.textColor=UIColorFromRGB(0x959E97);
        _placeholderText.font = SYSTEMFONT(14);
        _placeholderText.text=@"";
        _placeholderText.numberOfLines = 0;
    }
    return _placeholderText;
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    DLogFunc;
    DLog(@"%@",text);
//    if (textView.text.length > self.maxTextCount && self.maxTextCount != 0) {
//        ShowHubMessage(@"您已超出可输入范围");
//        return NO;
//    }
//    return YES;
    
    if ([text rangeOfString:@"\n"].length > 0) {
        [textView resignFirstResponder];
        return NO;
    }
    NSString *string = nil;

    if (range.length > 0) {
        if ([textView.text length] >= range.location) {
            string = [textView.text substringToIndex:range.location];
        }
        string = [NSString stringWithFormat:@"%@%@", string, text];
        if ([textView.text length] > range.location + range.length) {
            string = [NSString stringWithFormat:@"%@%@", string, [textView.text substringFromIndex:range.location + range.length]];
        }
    } else {
            string = [NSString stringWithFormat:@"%@%@", textView.text, text];
    }
    if (string.length > self.maxTextCount && self.maxTextCount) {
        NSRange rangeIndex = [string rangeOfComposedCharacterSequenceAtIndex:self.maxTextCount];
        if (rangeIndex.length == 1) {//字数超限
            textView.text = [string substringToIndex:self.maxTextCount];
        }else{
        NSRange rangeRange = [string rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.maxTextCount)];
            textView.text = [string substringWithRange:rangeRange];
        }
            ShowHubMessage(@"您已超出可输入范围");
            return NO;
        }
        return YES;
}

//返回值为BOOL类型，YES表示允许替换，NO表示不允许
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

@end
